<template>
<div :style='{"width":"80%","padding":"20px 20px 40px","boxShadow":"0px 4px 10px 0px rgba(0,0,0,0.3)","margin":"20px auto","position":"relative","background":"#fff"}'>
    <el-form
	  :style='{"width":"100%","position":"relative","flexWrap":"wrap","display":"flex"}'
      class="add-update-preview"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="80px"
    >
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="活动名称" prop="huodongmingcheng">
            <el-input v-model="ruleForm.huodongmingcheng" 
                placeholder="活动名称" clearable :disabled=" false  ||ro.huodongmingcheng"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="活动类型" prop="huodongleixing">
            <el-input v-model="ruleForm.huodongleixing" 
                placeholder="活动类型" clearable :disabled=" false  ||ro.huodongleixing"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="活动地点" prop="huodongdidian">
            <el-input v-model="ruleForm.huodongdidian" 
                placeholder="活动地点" clearable :disabled=" false  ||ro.huodongdidian"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="班级名称" prop="banjimingcheng">
            <el-input v-model="ruleForm.banjimingcheng" 
                placeholder="班级名称" clearable :disabled=" false  ||ro.banjimingcheng"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="活动图片" v-if="type!='cross' || (type=='cross' && !ro.huodongtupian)" prop="huodongtupian">
            <file-upload
            tip="点击上传活动图片"
            action="file/upload"
            :limit="3"
            :multiple="true"
            :fileUrls="ruleForm.huodongtupian?ruleForm.huodongtupian:''"
            @change="huodongtupianUploadChange"
            ></file-upload>
          </el-form-item>
            <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' class="upload" v-else label="活动图片" prop="huodongtupian">
                <img v-if="ruleForm.huodongtupian.substring(0,4)=='http'" class="upload-img" style="margin-right:20px;" v-bind:key="index" :src="ruleForm.huodongtupian.split(',')[0]" width="100" height="100">
                <img v-else class="upload-img" style="margin-right:20px;" v-bind:key="index" v-for="(item,index) in ruleForm.huodongtupian.split(',')" :src="baseUrl+item" width="100" height="100">
            </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}'  label="活动评星" prop="huodongpingxing">
            <el-select v-model="ruleForm.huodongpingxing" placeholder="请选择活动评星" :disabled=" false  ||ro.huodongpingxing" >
              <el-option
                  v-for="(item,index) in huodongpingxingOptions"
                  :key="index"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="学员学号" prop="xueyuanxuehao">
            <el-input v-model="ruleForm.xueyuanxuehao" 
                placeholder="学员学号" clearable :disabled=" false  ||ro.xueyuanxuehao"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="学员姓名" prop="xueyuanxingming">
            <el-input v-model="ruleForm.xueyuanxingming" 
                placeholder="学员姓名" clearable :disabled=" false  ||ro.xueyuanxingming"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="教辅工号" prop="jiaofugonghao">
            <el-input v-model="ruleForm.jiaofugonghao" 
                placeholder="教辅工号" clearable :disabled=" false  ||ro.jiaofugonghao"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="教辅姓名" prop="jiaofuxingming">
            <el-input v-model="ruleForm.jiaofuxingming" 
                placeholder="教辅姓名" clearable :disabled=" false  ||ro.jiaofuxingming"></el-input>
          </el-form-item>
          <el-form-item :style='{"width":"100%","padding":"10px","margin":"0 0 10px","background":"none"}' label="活动评价" prop="huodongpingjia">
            <el-input
              type="textarea"
              :rows="8"
              placeholder="活动评价"
              v-model="ruleForm.huodongpingjia">
            </el-input>
          </el-form-item>

      <el-form-item :style='{"padding":"0","margin":"0"}'>
        <el-button :style='{"border":"0","cursor":"pointer","padding":"0 20px","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"#3B2E7E","width":"auto","lineHeight":"40px","fontSize":"14px","height":"40px"}'  type="primary" @click="onSubmit">提交</el-button>
        <el-button :style='{"border":"0","cursor":"pointer","padding":"0 20px","margin":"0","outline":"none","color":"#000","borderRadius":"4px","background":"#9E9E9E","width":"auto","lineHeight":"40px","fontSize":"14px","height":"40px"}' @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
</div>
</template>

<script>
  export default {
    data() {
	  let self = this
      return {
        id: '',
        baseUrl: '',
        ro:{
				huodongmingcheng : false,
				huodongleixing : false,
				huodongdidian : false,
				banjimingcheng : false,
				huodongtupian : false,
				huodongpingxing : false,
				huodongpingjia : false,
				xueyuanxuehao : false,
				xueyuanxingming : false,
				jiaofugonghao : false,
				jiaofuxingming : false,
				crossuserid : false,
				crossrefid : false,
        },
        type: '',
        userTableName: localStorage.getItem('UserTableName'),
        ruleForm: {
          huodongmingcheng: '',
          huodongleixing: '',
          huodongdidian: '',
          banjimingcheng: '',
          huodongtupian: '',
          huodongpingxing: '',
          huodongpingjia: '',
          xueyuanxuehao: '',
          xueyuanxingming: '',
          jiaofugonghao: '',
          jiaofuxingming: '',
          crossuserid: '',
          crossrefid: '',
        },
        huodongpingxingOptions: [],


        rules: {
          huodongmingcheng: [
          ],
          huodongleixing: [
          ],
          huodongdidian: [
          ],
          banjimingcheng: [
          ],
          huodongtupian: [
          ],
          huodongpingxing: [
          ],
          huodongpingjia: [
          ],
          xueyuanxuehao: [
          ],
          xueyuanxingming: [
          ],
          jiaofugonghao: [
          ],
          jiaofuxingming: [
          ],
          crossuserid: [
          ],
          crossrefid: [
          ],
        },
		centerType: false,
      };
    },
    computed: {



    },
    components: {
    },
    created() {
		if(this.$route.query.centerType){
			this.centerType = true
		}
	  //this.bg();
      let type = this.$route.query.type ? this.$route.query.type : '';
      this.init(type);
      this.baseUrl = this.$config.baseUrl;
    },
    methods: {
      getMakeZero(s) {
          return s < 10 ? '0' + s : s;
      },
      // 下载
      download(file){
        window.open(`${file}`)
      },
      // 初始化
      init(type) {
        this.type = type;
        if(type=='cross'){
          var obj = JSON.parse(localStorage.getItem('crossObj'));
          for (var o in obj){
            if(o=='huodongmingcheng'){
              this.ruleForm.huodongmingcheng = obj[o];
              this.ro.huodongmingcheng = true;
              continue;
            }
            if(o=='huodongleixing'){
              this.ruleForm.huodongleixing = obj[o];
              this.ro.huodongleixing = true;
              continue;
            }
            if(o=='huodongdidian'){
              this.ruleForm.huodongdidian = obj[o];
              this.ro.huodongdidian = true;
              continue;
            }
            if(o=='banjimingcheng'){
              this.ruleForm.banjimingcheng = obj[o];
              this.ro.banjimingcheng = true;
              continue;
            }
            if(o=='huodongtupian'){
              this.ruleForm.huodongtupian = obj[o].split(",")[0];
              this.ro.huodongtupian = true;
              continue;
            }
            if(o=='huodongpingxing'){
              this.ruleForm.huodongpingxing = obj[o];
              this.ro.huodongpingxing = true;
              continue;
            }
            if(o=='huodongpingjia'){
              this.ruleForm.huodongpingjia = obj[o];
              this.ro.huodongpingjia = true;
              continue;
            }
            if(o=='xueyuanxuehao'){
              this.ruleForm.xueyuanxuehao = obj[o];
              this.ro.xueyuanxuehao = true;
              continue;
            }
            if(o=='xueyuanxingming'){
              this.ruleForm.xueyuanxingming = obj[o];
              this.ro.xueyuanxingming = true;
              continue;
            }
            if(o=='jiaofugonghao'){
              this.ruleForm.jiaofugonghao = obj[o];
              this.ro.jiaofugonghao = true;
              continue;
            }
            if(o=='jiaofuxingming'){
              this.ruleForm.jiaofuxingming = obj[o];
              this.ro.jiaofuxingming = true;
              continue;
            }
            if(o=='crossuserid'){
              this.ruleForm.crossuserid = obj[o];
              this.ro.crossuserid = true;
              continue;
            }
            if(o=='crossrefid'){
              this.ruleForm.crossrefid = obj[o];
              this.ro.crossrefid = true;
              continue;
            }
          }
        }else if(type=='edit'){
			this.info()
		}
        // 获取用户信息
        this.$http.get(this.userTableName + '/session', {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            var json = res.data.data;
            if((json.jiaofugonghao!=''&&json.jiaofugonghao) || json.jiaofugonghao==0){
                this.ruleForm.jiaofugonghao = json.jiaofugonghao;
				this.ro.jiaofugonghao = true;
            }
            if((json.jiaofuxingming!=''&&json.jiaofuxingming) || json.jiaofuxingming==0){
                this.ruleForm.jiaofuxingming = json.jiaofuxingming;
				this.ro.jiaofuxingming = true;
            }
          }
        });
        this.huodongpingxingOptions = "★★★★★,★★★★,★★★,★★,★".split(',')

		if (localStorage.getItem('raffleType') && localStorage.getItem('raffleType') != null) {
			localStorage.removeItem('raffleType')
			setTimeout(() => {
				this.onSubmit()
			}, 300)
		}
      },

    // 多级联动参数
      // 多级联动参数
      info() {
        this.$http.get(`huodongpingjia/detail/${this.$route.query.id}`, {emulateJSON: true}).then(res => {
          if (res.data.code == 0) {
            this.ruleForm = res.data.data;
          }
        });
      },
      // 提交
      onSubmit() {
			//更新跨表属性
			var crossuserid;
			var crossrefid;
			var crossoptnum;
			this.$refs["ruleForm"].validate(valid => {
				if(valid) {
					if(this.type=='cross'){
						var statusColumnName = localStorage.getItem('statusColumnName');
						var statusColumnValue = localStorage.getItem('statusColumnValue');
						if(statusColumnName && statusColumnName!='') {
							var obj = JSON.parse(localStorage.getItem('crossObj'));
							if(!statusColumnName.startsWith("[")) {
								for (var o in obj){
									if(o==statusColumnName){
										obj[o] = statusColumnValue;
									}
								}
								var table = localStorage.getItem('crossTable');
								this.$http.post(table+'/update', obj).then(res => {});
							} else {
								crossuserid=Number(localStorage.getItem('frontUserid'));
								crossrefid=obj['id'];
								crossoptnum=localStorage.getItem('statusColumnName');
								crossoptnum=crossoptnum.replace(/\[/,"").replace(/\]/,"");
							}
						}
					}
					if(crossrefid && crossuserid) {
						this.ruleForm.crossuserid=crossuserid;
						this.ruleForm.crossrefid=crossrefid;
						var params = {
							page: 1,
							limit: 10,
							crossuserid:crossuserid,
							crossrefid:crossrefid,
						}
						this.$http.get('huodongpingjia/list', {
							params: params
						}).then(res => {
							if(res.data.data.total>=crossoptnum) {
								this.$message({
									message: localStorage.getItem('tips'),
									type: 'error',
									duration: 1500,
								});
								return false;
							} else {
								// 跨表计算


								this.$http.post(`huodongpingjia/${this.ruleForm.id?'update':this.centerType?'save':'add'}`, this.ruleForm).then(res => {
									if (res.data.code == 0) {
										this.$message({
											message: '操作成功',
											type: 'success',
											duration: 1500,
											onClose: () => {
												this.$router.go(-1);
											}
										});
									} else {
										this.$message({
											message: res.data.msg,
											type: 'error',
											duration: 1500
										});
									}
								});
							}
						});
					} else {


						this.$http.post(`huodongpingjia/${this.ruleForm.id?'update':this.centerType?'save':'add'}`, this.ruleForm).then(res => {
							if (res.data.code == 0) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1500,
									onClose: () => {
										this.$router.go(-1);
									}
								});
							} else {
								this.$message({
									message: res.data.msg,
									type: 'error',
									duration: 1500
								});
							}
						});
					}
				}
			});
		},
		// 获取uuid
		getUUID () {
			return new Date().getTime();
		},
		// 返回
		back() {
			this.$router.go(-1);
		},
      huodongtupianUploadChange(fileUrls) {
          this.ruleForm.huodongtupian = fileUrls.replace(new RegExp(this.$config.baseUrl,"g"),"");
      },
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	.el-date-editor.el-input {
		width: auto;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__label {
	  padding: 0 10px 0 0;
	  color: #666;
	  font-weight: 500;
	  width: 80px;
	  font-size: 14px;
	  line-height: 40px;
	  text-align: right;
	}
	
	.add-update-preview .el-form-item /deep/ .el-form-item__content {
	  margin-left: 80px;
	}
	
	.add-update-preview .el-input /deep/ .el-input__inner {
	  border: 1px solid #DADADA;
	  border-radius: 0;
	  padding: 0 12px;
	  box-shadow: none;
	  outline: none;
	  color: #000;
	  width: 400px;
	  font-size: 14px;
	  height: 40px;
	}
	.add-update-preview .el-input-number /deep/ .el-input__inner {
		text-align: left;
	  border: 1px solid #DADADA;
	  border-radius: 0;
	  padding: 0 12px;
	  box-shadow: none;
	  outline: none;
	  color: #000;
	  width: 400px;
	  font-size: 14px;
	  height: 40px;
	}
	.add-update-preview .el-input-number /deep/ .el-input-number__decrease {
		display: none;
	}
	.add-update-preview .el-input-number /deep/ .el-input-number__increase {
		display: none;
	}
	
	.add-update-preview .el-select /deep/ .el-input__inner {
	  border: 1px solid #DADADA;
	  border-radius: 0;
	  padding: 0 10px;
	  box-shadow: none;
	  outline: none;
	  color: #000;
	  width: 400px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview .el-date-editor /deep/ .el-input__inner {
	  border: 1px solid #DADADA;
	  border-radius: 0;
	  padding: 0 10px 0 30px;
	  box-shadow: none;
	  outline: none;
	  color: #000;
	  width: 400px;
	  font-size: 14px;
	  height: 40px;
	}
	
	.add-update-preview /deep/ .el-upload--picture-card {
		background: transparent;
		border: 0;
		border-radius: 0;
		width: auto;
		height: auto;
		line-height: initial;
		vertical-align: middle;
	}
	
	.add-update-preview /deep/ .upload .upload-img {
	  border: 1px solid #DADADA;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #DADADA;
	  width: 200px;
	  font-size: 32px;
	  line-height: 60px;
	  text-align: center;
	  height: 60px;
	}
	
	.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
	  border: 1px solid #DADADA;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #DADADA;
	  width: 200px;
	  font-size: 32px;
	  line-height: 60px;
	  text-align: center;
	  height: 60px;
	}
	
	.add-update-preview /deep/ .el-upload .el-icon-plus {
	  border: 1px solid #DADADA;
	  cursor: pointer;
	  border-radius: 6px;
	  color: #DADADA;
	  width: 200px;
	  font-size: 32px;
	  line-height: 60px;
	  text-align: center;
	  height: 60px;
	}
	
	.add-update-preview .el-textarea /deep/ .el-textarea__inner {
	  border: 1px solid #DADADA;
	  border-radius: 0;
	  padding: 12px;
	  box-shadow: none;
	  outline: none;
	  color: #000;
	  width: 400px;
	  font-size: 14px;
	  height: 120px;
	}
</style>
